/*
 * Copyright (C) 2008 The Android Open Source Project
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

 /** @defgroup termios Termios
  * @ingroup libc
  */

#ifndef _TERMIOS_H_
#define _TERMIOS_H_

#include <sys/cdefs.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#ifndef __LITEOS__
#include <linux/termios.h>
#else
#include <liteos/termios.h>
#endif

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */


#ifndef __LITEOS__
#if __ANDROID_API__ >= 21
speed_t cfgetispeed(const struct termios*);
speed_t cfgetospeed(const struct termios*);
void cfmakeraw(struct termios*);
int cfsetispeed(struct termios*, speed_t);
int cfsetospeed(struct termios*, speed_t);
int cfsetspeed(struct termios*, speed_t);
int tcdrain(int);
int tcflow(int, int);
int tcflush(int, int);
int tcgetattr(int, struct termios*);
pid_t tcgetsid(int);
int tcsendbreak(int, int);
int tcsetattr(int, int, const struct termios*);
#else
#include <android/legacy_termios_inlines.h>
#endif
#else
/**
*@ingroup  termios
* 
*@par Description: 
*The cfgetispeed() function shall extract the input baud rate from the termios 
*structure to which the s argument points. This function shall return 
*exactly the value in the termios data structure, without interpretation.
*
*@attention
* <ul>
* <li>None.</li>
* </ul>
*
*@retval #speed_t Upon successful completion, cfgetispeed() shall return a value of 
*type speed_t representing the input baud rate.
*
*@par Dependency:
*<ul><li>termios.h</li></ul>
*@see cfgetospeed | cfsetispeed | cfsetospeed | tcgetattr
*@since Huawei LiteOS V100R001C00
*/
speed_t cfgetispeed(const struct termios* s);
/**
*@ingroup  termios
* 
*@par Description: 
*The cfgetospeed() function shall extract the output baud rate from the 
*termios structure to which the s argument points. This function 
*shall return exactly the value in the termios data structure, without interpretation.
*
*@attention
* <ul>
* <li>None.</li>
* </ul>
*
*@retval #speed_t Upon successful completion, cfgetospeed() shall return a value 
*of type speed_t representing the output baud rate.
*
*@par Dependency:
*<ul><li>termios.h</li></ul>
*@see cfgetispeed 
| cfsetispeed 
| cfsetospeed
 
| tcgetattr
*@since Huawei LiteOS V100R001C00
*/
speed_t cfgetospeed(const struct termios* s);
/**
*@ingroup  termios
* 
*@par Description: 
*The cfmakeraw() sets the terminal to something like the "raw" mode of terminal driver: 
*input is available character by character, echoing is disabled, and all special processing 
*of terminal input and output characters is disabled.
*
*@attention
*<ul>
*<li>
*The cfmakeraw() function is not a standard C Library Function.
*</li>
*</ul>
*
*@retval #void None.
*
*@par Dependency:
*<ul><li>termios.h</li></ul>
*
*@see cfgetispeed 
| cfsetispeed 
| cfsetospeed
 
| tcgetattr
*
*@since Huawei LiteOS V100R001C00
*/
void cfmakeraw(struct termios* s);
/**
*@ingroup  termios
* 
*@par Description: 
*The cfsetispeed() function shall set the input baud rate stored in 
*the structure pointed to by s to speed.
*
*@attention
* <ul>
* <li>None.</li>
* </ul>
*
*@retval #0 cfsetispeed() shall return 0.
*
*@par Dependency:
*<ul><li>termios.h</li></ul>
*@see cfgetispeed 
| cfgetospeed | cfsetospeed 
| tcsetattr
*@since Huawei LiteOS V100R001C00
*/
int cfsetispeed(struct termios *s, speed_t  speed);
/**
*@ingroup  termios
* 
*@par Description: 
*The cfsetospeed() function shall set the output baud rate stored in 
*the structure pointed to by s to speed.
*
*@attention
* <ul>
* <li>None.</li>
* </ul>
*
*@retval #0 cfsetospeed() shall return 0.
*
*@par Dependency:
*<ul><li>termios.h</li></ul>
*@see cfgetispeed 
| cfgetospeed | cfsetispeed | tcsetattr
*@since Huawei LiteOS V100R001C00
*/
int cfsetospeed(struct termios *s, speed_t  speed);
int cfsetspeed(struct termios*, speed_t);
int tcdrain(int);
/**
*@ingroup  termios
* 
*@par Description: 
*The tcflow() function shall suspend or restart transmission or reception of 
*data on the object referred to by fd, depending on the value of action.
*
*@attention
* <ul>
* <li>None.</li>
* </ul>
*
*@retval #0 Upon successful completion, 0 shall be returned. 
*@retval #-1 If it fails, -1 shall be returned and errno set to indicate the error.
*
*@par Errors
*<ul>
*<li><b>EBADF</b>:The fd argument is not a valid open file descriptor.</li>
*<li><b>EINVAL</b>:The request or arg argument is not valid for this device.</li>
*<li><b>ENOMEM</b>:If any resource allocation fails, ENOMEM is set as errno.</li>
*<li><b>EAFNOSUPPORT</b>:If socket created with PF_PACKET, SOCK_RAW is called with SIOCADDRT.</li>
*</ul>
*
*@par Dependency:
*<ul><li>termios.h</li></ul>
*@see tcsendbreak
*@since Huawei LiteOS V100R001C00
*/
int tcflow(int fd, int action);
/**
*@ingroup  termios
* 
*@par Description: 
*Upon successful completion, tcflush() shall discard data written to the object 
*referred to by fd (an open file descriptor associated with a terminal) but not transmitted, 
*or data received but not read, depending on the value of __queue.
*
*@attention
* <ul>
* <li>None.</li>
* </ul>
*
*@retval #0 Upon successful completion, 0 shall be returned. 
*@retval #-1 If it fails, -1 shall be returned and errno set to indicate the error.
*
*@par Errors
*<ul>
*<li><b>EBADF</b>:The fd argument is not a valid open file descriptor.</li>
*<li><b>EINVAL</b>:The request or arg argument is not valid for this device.</li>
*<li><b>ENOMEM</b>:If any resource allocation fails, ENOMEM is set as errno.</li>
*<li><b>EAFNOSUPPORT</b>:If socket created with PF_PACKET, SOCK_RAW is called with SIOCADDRT.</li>
*</ul>
*
*@par Dependency:
*<ul><li>termios.h</li></ul>
*@see None
*@since Huawei LiteOS V100R001C00
*/
int tcflush(int fd, int __queue);
/**
*@ingroup  termios
* 
*@par Description: 
*The tcgetattr() function shall get the parameters associated with the terminal referred 
*to by fd and store them in the termios structure referenced by s. 
*The fd argument is an open file descriptor associated with a terminal.
*
*@attention
* <ul>
* <li>None.</li>
* </ul>
*
*@retval #0 Upon successful completion, 0 shall be returned. 
*@retval #-1 If it fails, -1 shall be returned and errno set to indicate the error.
*
*@par Errors
*<ul>
*<li><b>EBADF</b>:The fd argument is not a valid open file descriptor.</li>
*<li><b>EINVAL</b>:The request or arg argument is not valid for this device.</li>
*<li><b>ENOMEM</b>:If any resource allocation fails, ENOMEM is set as errno.</li>
*<li><b>EAFNOSUPPORT</b>:If socket created with PF_PACKET, SOCK_RAW is called with SIOCADDRT.</li>
*</ul>
*
*@par Dependency:
*<ul><li>termios.h</li></ul>
*@see tcsetattr
*@since Huawei LiteOS V100R001C00
*/
int tcgetattr(int fd, struct termios *s);
/**
*@ingroup  termios
* 
*@par Description: 
*The tcgetsid() function shall obtain the process group ID of the session for which the terminal 
*specified by fildes is the controlling terminal.
*
*@attention
* <ul>
* <li>None.</li>
* </ul>
*
*@retval #pid_t Upon successful completion, tcgetsid() shall return the process group ID of 
*the session associated with the terminal. Otherwise, a value of -1 shall be returned 
*and errno set to indicate the error.
*
*@par Errors
*<ul>
*<li><b>EBADF</b>:The fd argument is not a valid open file descriptor.</li>
*<li><b>EINVAL</b>:The request or arg argument is not valid for this device.</li>
*<li><b>ENOMEM</b>:If any resource allocation fails, ENOMEM is set as errno.</li>
*<li><b>EAFNOSUPPORT</b>:If socket created with PF_PACKET, SOCK_RAW is called with SIOCADDRT.</li>
*</ul>
*
*@par Dependency:
*<ul><li>termios.h</li></ul>
*@see None
*@since Huawei LiteOS V100R001C00
*/
pid_t tcgetsid(int fd);
/**
*@ingroup  termios
* 
*@par Description: 
*The tcsendbreak() function sends a break for a specific duration.
*
*@attention
* <ul>
* <li>None.</li>
* </ul>
*
*@retval #0 Upon successful completion, 0 shall be returned. 
*@retval #-1 If it fails, -1 shall be returned and errno set to indicate the error.
*
*@par Errors
*<ul>
*<li><b>EBADF</b>:The fd argument is not a valid open file descriptor.</li>
*<li><b>EINVAL</b>:The request or arg argument is not valid for this device.</li>
*<li><b>ENOMEM</b>:If any resource allocation fails, ENOMEM is set as errno.</li>
*<li><b>EAFNOSUPPORT</b>:If socket created with PF_PACKET, SOCK_RAW is called with SIOCADDRT.</li>
*</ul>
*
*@par Dependency:
*<ul><li>termios.h</li></ul>
*@see None
*@since Huawei LiteOS V100R001C00
*/
int tcsendbreak(int fd, int __duration);
/**
*@ingroup  termios
* 
*@par Description: 
*The tcsetattr() function shall set the parameters associated with the terminal 
*referred to by the open file descriptor fd (an open file descriptor associated with a terminal) 
*from the termios structure referenced by s.
*
*@attention
* <ul>
* <li>None.</li>
* </ul>
*
*@retval #0 Upon successful completion, 0 shall be returned. 
*@retval #-1 If it fails, -1 shall be returned and errno set to indicate the error.
*
*@par Errors
*<ul>
*<li><b>EBADF</b>:The fd argument is not a valid open file descriptor.</li>
*<li><b>EINVAL</b>:The request or arg argument is not valid for this device.</li>
*<li><b>ENOMEM</b>:If any resource allocation fails, ENOMEM is set as errno.</li>
*<li><b>EAFNOSUPPORT</b>:If socket created with PF_PACKET, SOCK_RAW is called with SIOCADDRT.</li>
*</ul>
*
*@par Dependency:
*<ul><li>termios.h</li></ul>
*@see cfgetispeed 
| tcgetattr
*@since Huawei LiteOS V100R001C00
*/
int tcsetattr(int fd, int __opt, const struct termios *s);
#endif


#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */

#endif /* _TERMIOS_H_ */
